package com.bkfm.framework.sys.ctrl;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yak.core.web.BaseController;

import com.bkfm.framework.sys.entity.BkfmMenu;
import com.bkfm.framework.sys.entity.BkfmThemes;
import com.bkfm.framework.sys.entity.BkfmUsers;
import com.bkfm.framework.sys.utils.BkfmUserUtils;

@Controller
public class BkfmGolbalCtrl extends BaseController<BkfmUsers>{

	/**
	 * 登录页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/login")
	public String login(Model model) {
		return "global/login";
	}
	
	/**
	 * 主页
	 * @param model
	 * @return
	 */
	@RequestMapping("/index")
	public String index(Model model) {
		BkfmThemes themes = BkfmUserUtils.getCurrentUser().getBkfmThemes();
		if(themes == null || StringUtils.isEmpty(themes.getThemes_template())) {
			return "web/theme/system/index";
		}
		return themes.getThemes_template();
	}
	
	/**
	 * 跳转菜单
	 * @param menu
	 * @param model
	 * @return
	 */
	@RequestMapping("/nav")
	public String nav(BkfmMenu menu,Model model) {
		try {
			for(BkfmMenu bkfmMenu : BkfmUserUtils.getCurrentUserMenu()) {
				if(menu.getRwid().equals(bkfmMenu.getRwid())) {
					return bkfmMenu.getMenu_url();
				}
			}
			return "common/404";
		}catch(Exception e) {
			logger.error("请求访问菜单异常",e);
			return "common/404";
		}
		
	}
	
	/**
	 * 退出系统
	 * @param model
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(Model model) {
		try{
			Subject sub = SecurityUtils.getSubject();
			sub.logout();
			session.invalidate();
		}catch(Exception e){
			logger.error("用户注销异常",e);
		}
		return "redirect:/login";		
	}
}
